import {createRouter, createWebHistory} from 'vue-router'

import LoginView from '../views/LoginView.vue'
import RegisterView from '../views/RegisterView.vue'

import LayoutView from '../views/LayoutView.vue'

import HomeIndexView from '../views/home/IndexView.vue'

import BookListView from '../views/book/ListView.vue'

import MineIndexView from '../views/mine/IndexView.vue'

import ShopDetailView from '../views/shop/DetailView.vue'

import AddressListView from '../views/address/ListView.vue' 
import AddressSaveView from '../views/address/SaveView.vue'

import BookDetailView from '../views/book/DetailView.vue'
import BookConfirmView from '../views/book/ConfirmView.vue'

import PayView from '../views/pay/PayView.vue'
import PaySuccessView from '../views/pay/SuccessView.vue'



const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: "/",
            component: LayoutView,
            redirect: "/home/index",
            children: [
                {
                    path: "/home/index",
                    component: HomeIndexView
                },
                {
                    path: "/mina/vip",
                    component: BookConfirmView
                },
                {
                    path: "/book/list",
                    component: BookListView
                },
                {
                    path: "/mine/index",
                    component: MineIndexView
                },
            ],
        },{
            path: '/login',
            name: 'loginView',
            component: LoginView
        },{
            path: '/register',
            name: 'registerView',
            component: RegisterView
        },{
            path: '/shop/detail',
            name: 'shopDetailView',
            component: ShopDetailView
        },{
            path: '/book/:num',
            name: 'bookDetailView',
            component: BookDetailView
        },{
            path: '/book/confirm',
            name: 'bookConfirm',
            component: BookConfirmView
        },{
            path: '/address/list',
            name: 'addressList',
            component: AddressListView
        },{
            path: '/address/save',
            name: 'addressSave',
            component: AddressSaveView
        },{
            path: '/pay/pay',
            name: 'payView',
            component: PayView
        },{
            path: '/pay/success',
            name: 'paySuccessView',
            component: PaySuccessView
        }
    ]
})


router.beforeEach((to, from, next) => {

    let token = sessionStorage.getItem("token");
    let toPath = to.path;
    if(toPath=="/login"){
        //判断是否已经登录过了
        if(token){
            next({"path":"/shop/detail"});
        }else{
            next();
        }
    }else{
        //判断是否已经登录
        if(token){
            //已经登录了
            next();
        }else{
            console.log(toPath);
            //如果跳转到需要登录的界面,则调整到登录界面
            if(toPath=="/home/index" || toPath=="/shop/detail"){
                next(); 
            }else{
                next({"path":"/login"});
            }
        }
    }
})


export default router
